//
//  HomeVC.swift
//  DianXia
//
//  Created by you&me on 2017/12/15.
//  Copyright © 2017年 you&me. All rights reserved.
//

import UIKit

enum  HomeEventType {
    case Repair  //维修
    case Support // 补货
    case Active  //开通
    case Unlive  //退服
}

class HomeVC: UIViewController {

    @IBOutlet weak var maintainBtn: UIButton!
    @IBOutlet weak var replenishmentBtn: UIButton!
    @IBOutlet weak var requestBtn: UIButton!
    @IBOutlet weak var requestNum: UILabel!
    @IBOutlet weak var dredgeBtn: UIButton!
    @IBOutlet weak var retreatBtn: UIButton!
    @IBOutlet weak var outLogin: UIButton!
    @IBOutlet weak var taskingBtn: UIButton!

    let dispose = DisposeBag()

    let homeVM = HomeVM.shareHomeVN
    var locationTimer : Timer?

    override func viewDidLoad() {
        super.viewDidLoad()

        taskingBtn.layer.borderWidth = 1
        taskingBtn.layer.borderColor = UIColor.lightGray.cgColor
        // 发起请求
        homeVM.getNeedAuthenList()

        //绑定
        homeVM.modelList.asObservable()
              .map { $0.count }
              .subscribe(onNext: { (numText) in
                if numText == 0 {
                    self.requestNum.isHidden = true
                }else {
                    self.requestNum.text = "\(numText)"
                    self.requestNum.isHidden = false
                }
              })
              .disposed(by: dispose)

       
        // 维护申请的操作
        maintainBtn.rx.tap
                   .subscribe({ [weak self] _ in self?.maintainBtnClick(.Repair) })
                   .disposed(by: dispose)
        // 补货的操作
        replenishmentBtn.rx.tap
                        .subscribe({ _ in self.replenishmentBtnClick() })
                        .disposed(by: dispose)
        //请求的审批
        requestBtn.rx.tap
            .subscribe({ _ in self.replenishmentBtnClick() })
            .disposed(by: dispose)
        //开通
        dredgeBtn.rx.tap
            .subscribe({ [weak self] _ in self?.maintainBtnClick(.Active) })
            .disposed(by: dispose)
        // 退服
        retreatBtn.rx.tap
            .subscribe({ [weak self] _ in self?.maintainBtnClick(.Unlive) })
            .disposed(by: dispose)
        // 退出登录
        outLogin.rx.tap
                .subscribe ({ _ in
                    let vc = UIStoryboard.initialViewControllerFromStoryboard(.Main) as! UIViewController
                    UserManager.shareManager.isLogin = false
                    UIApplication.shared.keyWindow?.rootViewController = vc
                })
                .disposed(by: dispose)
        // 进行中的任务
        taskingBtn.rx.tap
            .subscribe ({ _ in
                let vc = UIStoryboard.viewController(from: .Home, viewControllerClass: TasksVC.self)
                vc.taskType = .IngTask
                self.present(vc, animated: true, completion: nil)
            })
            .disposed(by: dispose)



    }

    fileprivate func maintainBtnClick( _ type: HomeEventType) {
         guard UserManager.shareManager.business == "维护" else {
             BCShowMessage.shareMessage.showTipMsg("对不起、您还没有该权利", time: 1)
              return
          }
        prentScanVC(type)
    }
    fileprivate func replenishmentBtnClick() {
        guard UserManager.shareManager.business == "补货" else {
            BCShowMessage.shareMessage.showTipMsg("对不起、您还没补货的权利", time: 1)
            return
        }
        prentScanVC(.Support)
    }

    fileprivate func prentScanVC( _ type: HomeEventType) {
        let vc = ScanVC()
        var style = LBXScanViewStyle()
        style.animationImage = UIImage(named: "CodeScan.bundle/qrcode_scan_light_green")
        vc.scanStyle = style
        vc.eventType = type
        self.present(vc, animated: true, completion: nil)
    }

}
